<template>
	<view class="content" :style="{'padding-top':top+'px','min-height':windowheight+'px'}">
		<Navbar title="每日签到" bgColor="#da708b" :backIcon="false" style="color: #ffffff;z-index: 1;"></Navbar>
		<view class="top_bg">
			<view class="heard">
				<image
					src="http://zzqjt.oss-cn-hangzhou.aliyuncs.com/templates/client/res/b603ceb007cf42938e722d8cd2edbd8f-my-bg.png">
				</image>
				<view class="points_abled">
					<view class="pota_txt2">连续签到</view>
					<view class="pota_txt1">{{continuousCheckIn}}<text>天</text></view>
				</view>
			</view>
			<view>
				<!-- <uni-calendar :selected="selected"></uni-calendar> -->
				<view>
					<picker style="border: none;" fields="month" mode="date" :value="yearmounth" @change="changeyear">
						<view class="uni-input">{{yearmounth}}</view>
					</picker>
				</view>
				<view class="week">
					<view>日</view>
					<view>一</view>
					<view>二</view>
					<view>三</view>
					<view>四</view>
					<view>五</view>
					<view>六</view>
				</view>
				<view class="day">
					<view v-for="(item,index) in date" :key="index" :style=" item.select===1 ? 'color: #e77c90;': ''">
						{{item.data}}
						<image
							src="http://zzqjt-test.oss-cn-hangzhou.aliyuncs.com/templates/client/res/3da1d6bcbd2c45b781d4b3b465edca98-%E5%AF%B9%E5%8F%B7.png"
							v-if="item.state===1">
						</image>
					</view>
		
				</view>
				<!-- <button :disabled="isSingToday" class="sign_btn" @tap.stop="signIn">{{isSingToday?'已签到':'立即签到'}}</button> -->
				<button :disabled="isSingToday" class="sign_btn" @tap.stop="signIn">{{isSingToday?'今日已签到':'立即签到'}}</button>
			</view>
		</view>
	</view>
</template>
<script setup>
	import { ref,onMounted } from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const baseUrl = getApp().globalData.baseUrl
	const title=ref('Hello')
	const windowheight=ref()
	const platform = ref()
	const statusBar = ref()
	const customBar = ref()
	const top = ref()
	const isSingToday = ref(false)
	const continuousCheckIn = ref(0)
	const selected = ref([])
	const currentDay = ref(new Date().getDate())
	const mouth = new Date().getMonth() + 1
	const yearmounth = ref(new Date().getFullYear() + "-" + mouth)
	const beginDate = ref(new Date(yearmounth.value).getDay())
	const numberOfDaysInCurrentMonth = ref(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0).getDate())
	const date = ref([])
	const changeyear = (e) => {
		console.log(e.detail.value)
		date.value = []
		yearmounth.value = e.detail.value
		beginDate.value = new Date(e.detail.value).getDay()
		numberOfDaysInCurrentMonth.value = new Date(new Date(e.detail.value).getFullYear(), new Date(e.detail.value)
			.getMonth() + 1, 0).getDate()
		console.log(beginDate.value, numberOfDaysInCurrentMonth.value)
		checkList(new Date(e.detail.value))
	}
	const dateFormat = () => {
		let date = new Date()
		let year = date.getFullYear()
		let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
		let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
		return year + '-' + month + '-' + day
	}
	
	const signIn = () => {
		uni.showModal({
			confirmText: "确定",
			showCancel: false,
			content: "您已获得5积分",
			title: "签到成功",
			success: () => {
				selected.value = uni.getStorageSync('signInfo')
				//签到数据更新到后台数据库
				uni.request({
					url: getApp().globalData.baseUrl + "/check",
					method: "GET",
					header: {
						'content-type': 'application/json',
						'Access-Control-Allow-Origin': "*",
						'Authorization': uni.getStorageSync('token')
					},
					success: (res) => {
						console.log(res)
						isSingToday.value = true
						date.value = []
						checkList(new Date(yearmounth.value))
					}
				})
			}
		})
	}
	const checkList = (nowdate) => {
		for (let i = 0; i < beginDate.value; i++) {
			date.value.push({
				data: "",
				state: "",
				select: 0
			})
		}
		for (let i = 0; i < numberOfDaysInCurrentMonth.value; i++) {
			if (i === currentDay.value - 1 && nowdate.getMonth() === new Date().getMonth()) {
				date.value.push({
					data: i + 1,
					state: 0,
					select: 1
				})
			} else {
				date.value.push({
					data: i + 1,
					state: 0,
					select: 0
				})
			}
	
		}
		console.log(date.value)
		uni.request({
			url: getApp().globalData.baseUrl + "/check/list",
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Access-Control-Allow-Origin': "*",
				'Authorization': uni.getStorageSync('token')
			},
			data: {
				checkMonth: nowdate.getMonth() + 1,
				checkYear: nowdate.getFullYear(),
				clientNo: JSON.parse(uni.getStorageSync("user_info")).client_no
			},
			success: (res) => {
				console.log(res, res.data.value.length)
				//if (nowdate.getMonth() === new Date().getMonth()) {
				for (let i = 0; i < res.data.value.length; i++) {
					for (let j = 0; j < date.value.length; j++) {
						if (date.value[j].data === +res.data.value[i].checkDay) {
							console.log(date.value[j].data)
							date.value[j].state = 1
						}
					}
				}
				if (res.data.value.length === 0) {
					continuousCheckIn.value = 0
				} else if (res.data.value[res.data.value.length - 1].checkDayStatus === "TODAY") {
					continuousCheckIn.value = res.data.value[res.data.value.length - 1].checkSustainedNum
				} else if (res.data.value[res.data.value.length - 1].checkDayStatus === "YESTERDAY") {
					continuousCheckIn.value = res.data.value[res.data.value.length - 1].checkSustainedNum
				} else if (res.data.value[res.data.value.length - 1].checkDayStatus === "PASTDAY") {
					continuousCheckIn.value = 0
				}
				//}
			},
			fail: (err) => {
				console.log(err)
			}
		})
	}
	const getColumnHeight=()=>{
		uni.getSystemInfo({
			success(res) {
				console.log(res)
				platform.value = res.platform
				windowheight.value=res.screenHeight
				// #ifdef MP-WEIXIN
				statusBar.value = res.statusBarHeight
				const custom = wx.getMenuButtonBoundingClientRect()
				customBar.value = custom.bottom + custom.top - res.statusBarHeight
				// #endif
				// #ifdef APP-PLUS
				statusBar.value = res.statusBarHeight
				customBar.value = res.statusBarHeight + 45
				// 这里是在安卓手机上加上 3 像素（当时好像是在安卓水滴屏上，系统导航栏高度较低才加上去的，大家可以真机自己调试一下）
				if (res.platform == "android") {
					statusBar.value += 3
					customBar.value += 3
				}
				// #endif
			}
		})
		top.value = customBar.value
		windowheight.value=windowheight.value-top.value
		//console.log(statusBar.value, customBar.value, top.value,windowheight.value)
	}
	onShow(()=>{
		const now = new Date().getTime()
		if (!uni.getStorageSync('token') || now > uni.getStorageSync('expireTime')) {
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}else{
			//console.log(numberOfDaysInCurrentMonth.value, beginDate.value, currentDay.value)
			checkList(new Date())
			uni.request({
				url: getApp().globalData.baseUrl + "/check/status",
				method: "GET",
				header: {
					'content-type': 'application/json',
					'Access-Control-Allow-Origin': "*",
					'Authorization': uni.getStorageSync('token')
				},
				data: {},
				success: (res) => {
					console.log(res)
					isSingToday.value = res.data.value
				},
				fail: (err) => {
					console.log(err)
				}
			})
		}
	})
	onMounted(()=>{
		getColumnHeight()
	})
</script>
<style lang="less" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-image: linear-gradient(to bottom,#d95e7d,#ffffff);
	}
	.top_bg {
		width: 100%;
	}
	
	.heard {
		width: 100%;
		height: 400rpx;
		position: relative;
	
		image {
			height: 100%;
			width: 100%;
		}
	
		.points_abled {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin: auto;
			width: 30%;
			background: url("../../static/points_bg.png");
			background-repeat: no-repeat;
			background-size: 100%;
			height: 250rpx;
			color: #e77c90;
			//line-height: 60rpx;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
		}
	}
	
	.uni-input {
		width: 100%;
		height: 70rpx;
		text-align: center;
		line-height: 70rpx;
	}
	
	.week {
		display: flex;
		//justify-content: space-evenly;
		align-items: center;
	
		view {
			width: 14.28%;
			text-align: center;
			height: 70rpx;
			line-height: 70rpx;
		}
	}
	
	.day {
		display: flex;
		//justify-content: space-evenly;
		//align-items: center;
		flex-wrap: wrap;
	
		view {
			width: 14.28%;
			text-align: center;
			height: 107.1rpx;
			line-height: 107.1rpx;
			position: relative;
	
			image {
				height: 30rpx;
				width: 30rpx;
				position: absolute;
				top: 0;
				right: 0;
			}
		}
	}
	
	.pota_txt1 {
		font-size: 56rpx;
		font-weight: bold;
		display: flex;
		align-items: center;
		height: 74rpx;
		line-height: 74rpx;
	
		text {
			font-size: 40rpx;
			margin-top: 5rpx;
		}
	}
	
	.pota_txt2 {
		font-size: 28rpx;
	}
	
	.sign_days {
		background-color: #fff;
		width: 95%;
		border-radius: 10rpx;
		height: 300rpx;
		margin: auto;
		text-align: center;
	
	}
	
	.signd_tit {
		padding-top: 20rpx;
		font-size: 36rpx;
	}
	
	.signdc_txt1 {
		font-size: 78rpx;
		font-weight: bold;
	}
	
	.signdc_txt2 {
		font-size: 48rpx;
		margin-left: 20rpx;
	}
	
	.sign_btn {
		background-color: #e77c90;
		color: #fff;
		border-radius: 10rpx;
	}
</style>